package com.example.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.domain.Girls;
import com.example.myapplication.R;
import com.example.util.TimeUtil;
import com.thefinestartist.finestwebview.FinestWebView;

import java.util.List;


/**
 * Created by Administrator on 2016/5/23.
 */
public class SimpleRecyclerViewAdapter extends RecyclerView.Adapter<SimpleRecyclerViewAdapter.MyHolder> {
    private final Context context;
    private final List<Girls.ResultsBean> resultsBeen;

    public SimpleRecyclerViewAdapter(Context context, List<Girls.ResultsBean> resultsBeen) {
        this.context = context;
        this.resultsBeen = resultsBeen;
    }


    @Override
    public SimpleRecyclerViewAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        SimpleRecyclerViewAdapter.MyHolder myHolder = new SimpleRecyclerViewAdapter.MyHolder(LayoutInflater.from(context).inflate(R.layout.item_ganhuo_1,parent,false));
        return myHolder;
    }

    @Override
    public void onBindViewHolder(SimpleRecyclerViewAdapter.MyHolder holder, int position) {
        final String desc = resultsBeen.get(position).getDesc();
        String person = resultsBeen.get(position).getWho();
        String time = resultsBeen.get(position).getCreatedAt();
        final String url = resultsBeen.get(position).getUrl();
        String type = resultsBeen.get(position).getType();
        String timeConversion = TimeUtil.timeConversion(time);
        holder.personTextView.setText("❤:"+person);
        holder.timeTextView.setText("✍"+timeConversion);
        String symbol = getSymbol(type);
        holder.ganhuoTextView.setText(symbol+" "+desc);
        holder.ganhuoTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new FinestWebView.Builder(context)
                        .titleDefault(desc)
                        .swipeRefreshColorRes(android.R.color.holo_blue_bright)
                        .titleColorRes(R.color.finestWhite)
                        .urlColorRes(R.color.finestWhite)
                        .toolbarColorRes(R.color.colorCyana)
                        .statusBarColorRes(R.color.colorCyan)
                        .iconDefaultColorRes(R.color.finestWhite)
                        .progressBarColorRes(R.color.finestWhite)
                        .show(url);
            }
        });
    }

    private String getSymbol(String type) {
        switch (type){
            case "Android":
                return "✿";
            case "休息视频":
                return "❀";
            case "拓展资源":
                return "❃";
            case "前端":
                return "❁";
        }
        return "";
    }

    @Override
    public int getItemCount() {
        return resultsBeen.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        private TextView timeTextView;
        private TextView ganhuoTextView;
        private TextView personTextView;
        public MyHolder(View itemView) {
            super(itemView);
            ganhuoTextView = (TextView)itemView.findViewById(R.id.item_ganhuo_tv);
            timeTextView = (TextView)itemView.findViewById(R.id.item_time_tv);
            personTextView = (TextView)itemView.findViewById(R.id.recommendedPerson);
        }
    }
}
